package pl.edu.agh.ros.node;

import org.ros.node.Node;
import org.ros.node.NodeMain;

import pl.edu.agh.ros.node.conf.ProxyNodeConfiguration;
import pl.edu.agh.ros.node.conf.XMPPConfiguration;
import pl.edu.agh.ros.node.manager.IServerManager;
import pl.edu.agh.ros.node.manager.XMPPManager;

import com.google.common.base.Preconditions;

public abstract class AbstractProxyNode implements NodeMain {

	protected final IServerManager serverManager;
	protected final XMPPConfiguration xmppConfiguration;
	protected final ProxyNodeConfiguration proxyNodeConfiguration;
	protected final String nodeName;

	protected XMPPManager xmppConnector;
	protected Node node;

	public AbstractProxyNode(IServerManager serverManager, XMPPConfiguration xmppConfiguration,
			ProxyNodeConfiguration proxyNodeConfiguration, String nodeName) {
		Preconditions.checkNotNull(proxyNodeConfiguration);
		Preconditions.checkNotNull(xmppConfiguration);
		Preconditions.checkNotNull(nodeName);

		this.serverManager = serverManager;
		this.xmppConfiguration = xmppConfiguration;
		this.proxyNodeConfiguration = proxyNodeConfiguration;
		this.nodeName = nodeName;
	}

	@Override
	public void shutdown() {
		if (xmppConnector != null && xmppConnector.getConnection() != null) {
			xmppConnector.disconnect();
		}
		node.shutdown();
		node = null;
	}

}
